<template>
  <div>
    <!-- 表单查询 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline" size="mini">
      <el-form-item label="姓名">
        <el-input v-model="formInline.name" placeholder="请输入姓名查询"></el-input>
      </el-form-item>
      <el-form-item >
    
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="find">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
       <el-table
      :data="compData"
      style="width: 100%">
      <el-table-column prop="name" label="姓名" align="center"></el-table-column>
      <el-table-column prop="age" label="年龄" align="center"></el-table-column>
      <el-table-column prop="sex_text" label="性别" align="center"></el-table-column>
      <el-table-column prop="number" label="学号" align="center"></el-table-column>
      <el-table-column prop="class" label="班级" align="center"></el-table-column>
      <el-table-column prop="state_text" label="入学状态" align="center"></el-table-column>
      <el-table-column prop="address" label="地址" align="center"></el-table-column>
      <el-table-column prop="phone" label="手机号码" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
        <el-button type="danger" size="mini" icon="el-icon-delete" @click="del(scope.row)"></el-button>
      </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20, 30]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import {students} from '@/api/api.js'
import { studentDel } from '../../api/api'
export default {
  data() {
    return {
      tableData:[],
      total:0,//数据条数
      currentPage:1, //当前页数
      pageSize:5, //每页显示条数
      formInline: {
          name: '', //查询
          
        }
    }
  },
  created() {
    this.getData()
  },
  computed:{
    compData(){
      return this.tableData.slice((this.currentPage-1)*this.pageSize,this.currentPage*this.pageSize)
    }
  },
  methods: {
    // 提交查询
    find(){
      this.getData(this.formInline)
    },
    // 查询重置
    reset(){
      this.formInline={}
      this.getData(this.formInline)
    },
    // 分页
    handleSizeChange(val) {
      this.pageSize=val
        console.log(`每页 ${val} 条`);
      },
    handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.currentPage=val
    },
    // 获取数据
    getData(params){
      students(params).then((res)=>{
        if(res.data.status==200)
        this.tableData=res.data.data
        this.total=res.data.total
        this.tableData.forEach(item => {
          item.sex===1?item.sex_text='男':item.sex_text='女'
          item.state==="1"?item.state_text='已入学':item.state==="2"?item.state_text='未入学':item.state_text='休学中'
        });
      })

    },
    // 删除
    del(row){
      console.log(row);
      studentDel(row.id).then(res=>{
        console.log(res);
        if(res.data.status===200){
          this.$message({message:'删除数据成功',type:'success'})
          this.getData()
        }
      })
    }
  },
}
</script>

<style>

</style>